// components/navbar/navbar.js
const {
  follow
} = require('../../apis/api')

import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'

const App = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    name:String,
    img:String,
    relation:Boolean,
    userid:String,
    showNav:{
      type:Boolean,
      value:true
    },
  },
  lifetimes: {
    created: function() {
      this.storeBindings = createStoreBindings(this, {
        store,
        fields: ['username', 'userAvatar', 'email', 'token'],
        actions: ['setUsername', 'setUserAvatar', 'setEmail', 'setToken'],
      })
    },
    attached: function () {
      this.setData({
        navHeight: App.globalData.navHeight,
        navTop:App.globalData.navTop
      })
     }
  },
  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
      //回退
      navBack: function () {
        wx.navigateBack({
          delta: 1
        })      
    },
    gotoUser(){
      wx.navigateTo({
        url: '/pages/userDetail/userDetail?id='+this.data.userid,
      })
    },
    follow(){
      follow(this.data.token,this.data.userid,(res)=>{
        if(res.data.code===200)
        {
          wx.showToast({
            title: '关注成功',
            icon:'success'
          })
          this.setData({
            relation:true
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon:'none'
          })
        }
      })
    }
  }
})
